package com.shxzz.shortlink.project.utils;

import jakarta.servlet.http.HttpServletRequest;

/**
 * @author shxzz
 * @create 2023-12-26-15:15
 * @description 获取网络请求相关信息工具类
 */
public class RequestInfoUtil {
    private RequestInfoUtil() {
    }

    /**
     * 获取请求源 IP 地址
     */
    public static String getIPAddress(HttpServletRequest request) {
        // 代理头
        String[] headersToCheck = {
                "X-Forwarded-For",
                "Proxy-Client-IP",
                "WL-Proxy-Client-IP",
                "HTTP_CLIENT_IP",
                "X-Real-IP"
        };

        String clientIP = null;

        for (String header : headersToCheck) {
            String headerValue = request.getHeader(header);
            if (headerValue != null && headerValue.length() != 0 && !"unknown".equalsIgnoreCase(headerValue)) {
                clientIP = headerValue.split(",")[0].trim();
                break;
            }
        }

        // 如果代理头中都没有找到有效的IP地址，则使用getRemoteAddr()
        if (clientIP == null || clientIP.length() == 0 || "unknown".equalsIgnoreCase(clientIP)) {
            clientIP = request.getRemoteAddr();
        }

        // 如果是IPv6的本地回环地址，转换为IPv4的本地回环地址
        return clientIP.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : clientIP;
    }

    /**
     * 获取请求操作系统信息
     */
    public static String getOperatingSystem(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.toLowerCase().contains("windows")) {
            return "Windows";
        } else if (userAgent.toLowerCase().contains("mac")) {
            return "Mac OS";
        } else if (userAgent.toLowerCase().contains("linux")) {
            return "Linux";
        } else if (userAgent.toLowerCase().contains("android")) {
            return "Android";
        } else if (userAgent.toLowerCase().contains("iphone") || userAgent.toLowerCase().contains("ipad")) {
            return "iOS";
        } else {
            return "Unknown";
        }
    }

    /**
     * 获取请求使用浏览器信息
     */
    public static String getBrowser(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.toLowerCase().contains("edg")) {
            return "Microsoft Edge";
        } else if (userAgent.toLowerCase().contains("chrome")) {
            return "Google Chrome";
        } else if (userAgent.toLowerCase().contains("firefox")) {
            return "Mozilla Firefox";
        } else if (userAgent.toLowerCase().contains("safari")) {
            return "Apple Safari";
        } else if (userAgent.toLowerCase().contains("opera")) {
            return "Opera";
        } else if (userAgent.toLowerCase().contains("msie") || userAgent.toLowerCase().contains("trident")) {
            return "Internet Explorer";
        } else {
            return "Unknown";
        }
    }

    /**
     * 获取请求使用设备信息
     */
    public static String getDevice(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent != null) {
            // 进行设备信息的解析
            if (userAgent.toLowerCase().contains("mobile")) {
                return "Mobile";
            } else if (userAgent.toLowerCase().contains("tablet")) {
                return "Tablet";
            } else {
                return "PC";
            }
        }
        return "Unknown";
    }

    /**
     * 获取用户访问使用网络
     */
    public static String getNetwork(HttpServletRequest request) {
        String actualIp = getIPAddress(request);
        return actualIp.startsWith("192.168.")
                || actualIp.startsWith("10.")
                || actualIp.startsWith("172.")
                ? "WIFI" : "Mobile";
    }

}
